//
//  PGTopView.m
//  youqu
//
//  Created by 彭鹏 on 16/10/16.
//  Copyright © 2016年 pengpeng. All rights reserved.
//

#import "PGTopView.h"
#import "PGPostViewModel.h"
#import "PGPostItem.h"

#import <SDWebImage/UIImageView+WebCache.h>
@interface PGTopView ()
@property (weak, nonatomic) IBOutlet UIImageView *profile_imageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *passtimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *txtLabel;

@end

@implementation PGTopView
- (IBAction)moreButtonClick {
//    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"退出" otherButtonTitles:@"确定",nil];
//    [sheet showInView:self];
    
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确定" message:@"取消" preferredStyle:UIAlertControllerStyleActionSheet];
    NSLog(@"%@",[UIApplication sharedApplication].keyWindow.rootViewController);
    UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"111" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"222" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:alertAction1];
    [alertController addAction:alertAction2];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
    
}

+ (instancetype)topView {
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] firstObject];
}

- (void)setItem:(PGPostItem *)item {
    _item = item;
    
    [self.profile_imageView sd_setImageWithURL:[NSURL URLWithString:item.profile_image] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image == nil) return;
        self.profile_imageView.image = [image imageCicle:image];
    }];

    self.nameLabel.text = item.name;
    
    self.txtLabel.text = item.text;
//    2015-08-31 11:52:01
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *passDate = [formatter dateFromString:item.passtime];
    
    NSDate *currentDate = [NSDate date];
    
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    
    NSDateComponents *currentComponents = [currentCalendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitSecond fromDate:currentDate];
    NSDateComponents *passComponents = [currentCalendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitSecond fromDate:passDate];
//    NSDateComponents *detal = [currentCalendar components:NSCalendarUnitYear fromDate:currentDate toDate:passDate options:NSCalendarWrapComponents];
    

    NSString *passTimeText = item.passtime;
    
    if (currentComponents.year == passComponents.year) {
        //NSLog(@"今年");
        if (currentComponents.day==passComponents.day) {
            //今天
            if (currentComponents.hour==passComponents.hour) {
                //一小时之内
                if (currentComponents.second==passComponents.second){
                    //一分钟之内
                    passTimeText = @"刚刚";
                }else {
                    //多少分钟之前
                    passTimeText = [NSString stringWithFormat:@"%zd分钟之前",currentComponents.second-passComponents.second];
                }
            }else {
                //几小时之前
                passTimeText = [NSString stringWithFormat:@"%zd小时之前",currentComponents.hour-passComponents.hour];
            }
            
        }else {
            //昨天  几个月之前
            if (currentComponents.day-passComponents.day==1) {
                //昨天
                passTimeText = [NSString stringWithFormat:@"昨天 %zd:%zd",passComponents.hour,passComponents.second];
            }else {
                passTimeText = [NSString stringWithFormat:@"今年 %zd-%zd",passComponents.month,passComponents.day];
            }
        }
    }else {
        //NSLog(@"去年");
    }
    
    self.passtimeLabel.text = passTimeText;
    
    
}

@end
